/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package core.process;

import core.utils.EPara;
import core.utils.Utils;
import core.process.pack.NucsPack;
import core.rb.PB;
import core.rb.r.PR;
import core.sb.SB;
import gui.processor.Processor;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.concurrent.CountDownLatch;

/*Author: John Archer, Contact: john.archer.jpa@gmail.com*/
public class Nucs{
    private PB pb;
    private Processor gp;
    public Nucs(PB pBlock, Processor gp){this.pb = pBlock;this.gp = gp;}
    public int [][] getNucs(double filter) throws Exception{
        int size = (int)((pb.getSize() / EPara.THREADS));
        int i = 0;
        ArrayList <PB> packs = new ArrayList();
        ArrayList <PR> pack;
        for(int x = 0; x<EPara.THREADS; x++){
            pack = new ArrayList();
            for(int y = 0; y<size; y++){pack.add(pb.getRead(i++));}
            packs.add(new PB(pb.getRef(), pack));
        }
        PB fPack = packs.get(0);
        for(int x=i; x<pb.getSize(); x++){fPack.add(pb.getRead(i++));}
        NucsPack [] np = new NucsPack[packs.size()];
        CountDownLatch cdl = new CountDownLatch(packs.size());
        for (int x = 0; x < packs.size(); x++) {
            if(x==0){np[x] = new NucsPack(gp, cdl, packs.get(x), filter);}
            else{np[x] = new NucsPack(null, cdl, packs.get(x), filter);}
        }
        cdl.await();
        LinkedList <int[][]> packetFreqs = new LinkedList();
        for (int x = 0; x < np.length; x++){packetFreqs.add(np[x].getPackOut());}
        int[][] freqs = new int[SB.NUCS.length][pb.getRef().getDNA().length()];
        for (int x = 0; x < packetFreqs.size(); x++){Utils.add(packetFreqs.get(x), freqs);}
        return freqs;
    }
}